﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;

namespace DigitalHydraParticleEditor
{
    public partial class Form1 : Form
    {
        Emitter emitter;
        bool IsPressed = false;
        RandomizeToolBox RTB;
        float StartTime;
        double CurrnetTime;
        float PreviousTime;
        int frameCount;
        int FPS;
        int Source = (int)D3DBLEND.D3DBLEND_SRCALPHA;
        int Dest = (int)D3DBLEND.D3DBLEND_INVSRCALPHA;
        bool IsRectOn = false;
        Color RectColor = Color.Red;
        bool continous = true;
        int BGID = -1;
        string BGFilePath = "";
        string RelativePath = "../../resources/";
        string ParticleFile;

        private bool looping = true;

        public bool Looping
        {
            get { return looping; }
            set { looping = value; }
        }

        public Form1()
        {
            InitializeComponent();

            FolderBrowserDialog box = new FolderBrowserDialog();

            if (DialogResult.OK == box.ShowDialog())
            {
                RelativePath = box.SelectedPath;
            }

            OpenFileDialog dlg = new OpenFileDialog();
            dlg.InitialDirectory = RelativePath;
            if (DialogResult.OK == dlg.ShowDialog())
            {
                ParticleFile = dlg.FileName;
            }

            SGP.ManagedDirect3D.Instance.InitManagedDirect3D(graphicsPanel1,graphicsPanel1.Width,splitContainer1.Panel1.Height,true);

            SGP.ManagedTextureManager.Instance.InitManagedTextureManager(SGP.ManagedDirect3D.Instance.Device, 
                SGP.ManagedDirect3D.Instance.Sprite);

            XPosNumUpDown.Maximum = splitContainer1.Panel1.Width;
            YPosNumUpDown.Maximum = splitContainer1.Panel1.Height;
            StartColorButton.BackColor = Color.Blue;
            StartColorButton.ForeColor = Color.White;
            EndColorButton.BackColor = Color.White;
            XPosNumUpDown.Value = 300;
            YPosNumUpDown.Value = 200;
            emitter = new Emitter((int)EmitterWNumUpDown.Value, (int)EmitterHNumUpDown.Value,
                (int)XPosNumUpDown.Value, (int)YPosNumUpDown.Value,(int)MaxPartNumUpDown.Value,
                (int)LifeSpanNumUpDown.Value,(int)MaxLifeNumUpDown.Value 
                ,(float)StartScaleNumUpDown.Value,(float)EndScaleNumUpDown.Value,
                (float)XVelNumUpDown.Value, (float)XVel2NumUpDown.Value, 
                (float)YVelNumUpDown.Value, (float)YVel2NumUpDown.Value, 
                (float)RotationNumUpDown.Value,ParticleFile, (int)StartAlphaNumUpDown.Value,
                (int)EndAlphaNumUpDown.Value);

            StartTime = Environment.TickCount;
        }

        public void Render()
        {

            PreviousTime = StartTime;
            StartTime = Environment.TickCount;

            CurrnetTime = ((double)this.StartTime - (double)this.PreviousTime) / 1000.0f;

            emitter.Update(CurrnetTime, (int)EmitterWNumUpDown.Value, (int)EmitterHNumUpDown.Value,
                (int)XPosNumUpDown.Value, (int)YPosNumUpDown.Value, (int)MaxPartNumUpDown.Value,
                (int)LifeSpanNumUpDown.Value, (int)MaxLifeNumUpDown.Value, (float)StartScaleNumUpDown.Value, (float)EndScaleNumUpDown.Value,
                (float)XVelNumUpDown.Value, (float)XVel2NumUpDown.Value,
                (float)YVelNumUpDown.Value, (float)YVel2NumUpDown.Value,
                (float)RotationNumUpDown.Value, (float)GravityXNumUpDown.Value,
                (float)GravityYNumUpDown.Value, Source, Dest, this.continous,(int)StartAlphaNumUpDown.Value,
                (int)EndAlphaNumUpDown.Value);

            frameCount++;

            if (Environment.TickCount - CurrnetTime > 1000.0f)
            {
                FPS = frameCount;
                frameCount = 0;
            }

            SGP.ManagedDirect3D.Instance.Clear(graphicsPanel1, splitContainer1.Panel1.BackColor);
            SGP.ManagedDirect3D.Instance.DeviceBegin();
            SGP.ManagedDirect3D.Instance.SpriteBegin();
            SGP.ManagedDirect3D.Instance.Sprite.Flush();
            int destblend = (int)SGP.ManagedDirect3D.Instance.Device.RenderState.DestinationBlend;
            int srcblend = (int)SGP.ManagedDirect3D.Instance.Device.RenderState.SourceBlend;
            SGP.ManagedDirect3D.Instance.Device.RenderState.DestinationBlend = (Microsoft.DirectX.Direct3D.Blend)Dest;
            SGP.ManagedDirect3D.Instance.Device.RenderState.SourceBlend = (Microsoft.DirectX.Direct3D.Blend)Source;

            if (BGID >= 0)
                SGP.ManagedTextureManager.Instance.Draw(BGID, 0, 0, 1.0f, 1.0f, Rectangle.Empty, 0, 0, 0.0f, 0);

            if (IsRectOn == true)
                SGP.ManagedDirect3D.Instance.DrawEmptyRect(emitter.Rect, RectColor);
            
            emitter.Render();

            SGP.ManagedDirect3D.Instance.Sprite.Flush();
            SGP.ManagedDirect3D.Instance.Device.RenderState.DestinationBlend = (Microsoft.DirectX.Direct3D.Blend)destblend;
            SGP.ManagedDirect3D.Instance.Device.RenderState.SourceBlend = (Microsoft.DirectX.Direct3D.Blend)srcblend;
            SGP.ManagedDirect3D.Instance.SpriteEnd();
            SGP.ManagedDirect3D.Instance.DeviceEnd();
            SGP.ManagedDirect3D.Instance.Present();
        }

        // File
        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LifeSpanNumUpDown.Value = 1;
            LifeSpanTrackBar.Value = 1;

            StartScaleNumUpDown.Value = 1;
            StartScaleTrackBar.Value = 1;

            EndScaleNumUpDown.Value = 1;
            EndScaleTrackBar.Value = 1;

            XVelNumUpDown.Value = 100;
            XVelTrackBar.Value = 100;

            XVel2NumUpDown.Value = -100;
            XVel2TrackBar.Value = -100;

            YVelNumUpDown.Value = 100;
            YVelTrackBar.Value = 100;

            YVel2NumUpDown.Value = -100;
            YVel2TrackBar.Value = -100;

            MaxPartNumUpDown.Value = 200;
            MaxPartTrackBar.Value = 200;

            RotationNumUpDown.Value = 100;
            RotationTrackBar.Value = 100;

            EmitterWNumUpDown.Value = 200;
            EmitterWTrackBar.Value = 200;

            EmitterHNumUpDown.Value = 300;
            EmitterHTrackBar.Value = 300;

            GravityXNumUpDown.Value = 0;
            GravityXTrackBar.Value = 0;

            GravityYNumUpDown.Value = 0;
            GravityYTrackBar.Value = 0;

            SourceComboBox.Text = "SCRALPHA";
            DestComboBox.Text = "INVSCRALPHA";

            XPosNumUpDown.Maximum = splitContainer1.Panel1.Width;
            YPosNumUpDown.Maximum = splitContainer1.Panel1.Height;
            XPosNumUpDown.Value = 300;
            YPosNumUpDown.Value = 200;

            emitter = new Emitter((int)EmitterWNumUpDown.Value, (int)EmitterHNumUpDown.Value,
                (int)XPosNumUpDown.Value, (int)YPosNumUpDown.Value, (int)MaxPartNumUpDown.Value,
                (int)LifeSpanNumUpDown.Value, (int)MaxLifeNumUpDown.Value
                , (float)StartScaleNumUpDown.Value, (float)EndScaleNumUpDown.Value,
                (float)XVelNumUpDown.Value, (float)XVel2NumUpDown.Value,
                (float)YVelNumUpDown.Value, (float)YVel2NumUpDown.Value,
                (float)RotationNumUpDown.Value, ParticleFile,(int)StartAlphaNumUpDown.Value,
                (int)EndAlphaNumUpDown.Value);

            Source = (int)D3DBLEND.D3DBLEND_SRCALPHA;
            Dest = (int)D3DBLEND.D3DBLEND_INVSRCALPHA;
            IsRectOn = false;
            RectColor = Color.Red;
            continous = true;

            BGID = -1;

            StartColorButton.BackColor = Color.Blue;
            StartColorButton.ForeColor = Color.White;
            EndColorButton.BackColor = Color.White;
            EndColorButton.ForeColor = Color.Black;

        }

        private void saveParticleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "All Files|*.*|Xml Files|*.xml";
            dlg.FilterIndex = 2;
            dlg.DefaultExt = "xml";
            dlg.InitialDirectory = RelativePath;

            if (DialogResult.OK == dlg.ShowDialog())
            {
                XElement Root = new XElement("Particle");

                XElement Life = new XElement("Life");
                Root.Add(Life);

                    XAttribute MinLife = new XAttribute("MinLife", LifeSpanNumUpDown.Value);
                    Life.Add(MinLife);

                    XAttribute MaxLife = new XAttribute("MaxLife", MaxLifeNumUpDown.Value);
                    Life.Add(MaxLife);

                    XAttribute MaxParticles = new XAttribute("MaxParticles", MaxPartNumUpDown.Value);
                    Life.Add(MaxParticles);

                XElement Size = new XElement("Size");
                Root.Add(Size);

                    XAttribute StartScale = new XAttribute("StartScale", StartScaleNumUpDown.Value);
                    Size.Add(StartScale);

                    XAttribute EndScale = new XAttribute("EndScale", EndScaleNumUpDown.Value);
                    Size.Add(EndScale);

                XElement Velocity = new XElement("Velocity");
                Root.Add(Velocity);

                    XAttribute XVelocity1 = new XAttribute("XVelocity1", XVelNumUpDown.Value);
                    Velocity.Add(XVelocity1);

                    XAttribute XVelocity2 = new XAttribute("XVelocity2", XVel2NumUpDown.Value);
                    Velocity.Add(XVelocity2);

                    XAttribute YVelocity1 = new XAttribute("YVelocity1", YVelNumUpDown.Value);
                    Velocity.Add(YVelocity1);

                    XAttribute YVelocity2 = new XAttribute("YVelocity2", YVel2NumUpDown.Value);
                    Velocity.Add(YVelocity2);

                XElement Movement = new XElement("Movement");
                Root.Add(Movement);

                    XAttribute Rotation = new XAttribute("Rotation", RotationNumUpDown.Value);
                    Movement.Add(Rotation);

                    XAttribute GravityX = new XAttribute("GravityX", GravityXNumUpDown.Value);
                    Movement.Add(GravityX);

                    XAttribute GravityY = new XAttribute("GravityY", GravityYNumUpDown.Value);
                    Movement.Add(GravityY);

                XElement Blend = new XElement("Blend");
                Root.Add(Blend);

                switch (SourceComboBox.Text)
                {
                    case "ZERO":
                        XAttribute SourceBlend1 = new XAttribute("SourceBlend", 1);
                        Blend.Add(SourceBlend1);
                        break;
                    case "ONE":
                        XAttribute SourceBlend2 = new XAttribute("SourceBlend", 2);
                        Blend.Add(SourceBlend2);
                        break;
                    case "SRCCOLOR":
                        XAttribute SourceBlend3 = new XAttribute("SourceBlend", 3);
                        Blend.Add(SourceBlend3);
                        break;
                    case "INVSRCCOLOR":
                        XAttribute SourceBlend4 = new XAttribute("SourceBlend", 4);
                        Blend.Add(SourceBlend4);
                        break;
                    case "SRCALPHA":
                        XAttribute SourceBlend5 = new XAttribute("SourceBlend", 5);
                        Blend.Add(SourceBlend5);
                        break;
                    case "INVSRCALPHA":
                        XAttribute SourceBlend6 = new XAttribute("SourceBlend", 6);
                        Blend.Add(SourceBlend6);
                        break;
                    case "DESTALPHA":
                        XAttribute SourceBlend7 = new XAttribute("SourceBlend", 7);
                        Blend.Add(SourceBlend7);
                        break;
                    case "INVDESTALPHA":
                        XAttribute SourceBlend8 = new XAttribute("SourceBlend", 8);
                        Blend.Add(SourceBlend8);
                        break;
                    case "DESTCOLOR":
                        XAttribute SourceBlend9 = new XAttribute("SourceBlend", 9);
                        Blend.Add(SourceBlend9);
                        break;
                    case "INVDESTCOLOR":
                        XAttribute SourceBlend10 = new XAttribute("SourceBlend", 10);
                        Blend.Add(SourceBlend10);
                        break;
                    case "SRCALPHASAT":
                        XAttribute SourceBlend11 = new XAttribute("SourceBlend", 11);
                        Blend.Add(SourceBlend11);
                        break;
                    case "BOTHSRCALPHA":
                        XAttribute SourceBlend12 = new XAttribute("SourceBlend", 12);
                        Blend.Add(SourceBlend12);
                        break;
                    case "BOTHINVSRCALPHA":
                        XAttribute SourceBlend13 = new XAttribute("SourceBlend", 13);
                        Blend.Add(SourceBlend13);
                        break;
                    case "BLENDFACTOR":
                        XAttribute SourceBlend14 = new XAttribute("SourceBlend", 14);
                        Blend.Add(SourceBlend14);
                        break;
                    case "INVBLENDFACTOR":
                        XAttribute SourceBlend15 = new XAttribute("SourceBlend", 15);
                        Blend.Add(SourceBlend15);
                        break;
                    case "SRCCOLOR2":
                        XAttribute SourceBlend16 = new XAttribute("SourceBlend", 16);
                        Blend.Add(SourceBlend16);
                        break;
                    case "INVSRCCOLOR2":
                        XAttribute SourceBlend17 = new XAttribute("SourceBlend", 17);
                        Blend.Add(SourceBlend17);
                        break;
                };

                switch (DestComboBox.Text)
                {
                    case "ZERO":
                        XAttribute DestBlend1 = new XAttribute("DestBlend", 1);
                        Blend.Add(DestBlend1);
                        break;
                    case "ONE":
                        XAttribute DestBlend2 = new XAttribute("DestBlend", 2);
                        Blend.Add(DestBlend2);
                        break;
                    case "SRCCOLOR":
                        XAttribute DestBlend3 = new XAttribute("DestBlend", 3);
                        Blend.Add(DestBlend3);
                        break;
                    case "INVSRCCOLOR":
                        XAttribute DestBlend4 = new XAttribute("DestBlend", 4);
                        Blend.Add(DestBlend4);
                        break;
                    case "SRCALPHA":
                        XAttribute DestBlend5 = new XAttribute("DestBlend", 5);
                        Blend.Add(DestBlend5);
                        break;
                    case "INVSRCALPHA":
                        XAttribute DestBlend6 = new XAttribute("DestBlend", 6);
                        Blend.Add(DestBlend6);
                        break;
                    case "DESTALPHA":
                        XAttribute DestBlend7 = new XAttribute("DestBlend", 7);
                        Blend.Add(DestBlend7);
                        break;
                    case "INVDESTALPHA":
                        XAttribute DestBlend8 = new XAttribute("DestBlend", 8);
                        Blend.Add(DestBlend8);
                        break;
                    case "DESTCOLOR":
                        XAttribute DestBlend9 = new XAttribute("DestBlend", 9);
                        Blend.Add(DestBlend9);
                        break;
                    case "INVDESTCOLOR":
                        XAttribute DestBlend10 = new XAttribute("DestBlend", 10);
                        Blend.Add(DestBlend10);
                        break;
                    case "SRCALPHASAT":
                        XAttribute DestBlend11 = new XAttribute("DestBlend", 11);
                        Blend.Add(DestBlend11);
                        break;
                    case "BOTHSRCALPHA":
                        XAttribute DestBlend12 = new XAttribute("DestBlend", 12);
                        Blend.Add(DestBlend12);
                        break;
                    case "BOTHINVSRCALPHA":
                        XAttribute DestBlend13 = new XAttribute("DestBlend", 13);
                        Blend.Add(DestBlend13);
                        break;
                    case "BLENDFACTOR":
                        XAttribute DestBlend14 = new XAttribute("DestBlend", 14);
                        Blend.Add(DestBlend14);
                        break;
                    case "INVBLENDFACTOR":
                        XAttribute DestBlend15 = new XAttribute("DestBlend", 15);
                        Blend.Add(DestBlend15);
                        break;
                    case "SRCCOLOR2":
                        XAttribute DestBlend16 = new XAttribute("DestBlend", 16);
                        Blend.Add(DestBlend16);
                        break;
                    case "INVSRCCOLOR2":
                        XAttribute DestBlend17 = new XAttribute("DestBlend", 17);
                        Blend.Add(DestBlend17);
                        break;
                };

                XElement Loop = new XElement("Loop");
                Root.Add(Loop);

                if (this.continous == true)
                {
                    XAttribute Continous = new XAttribute("Continous", 1);
                    Loop.Add(Continous);
                }
                else if (this.continous == false)
                {
                    XAttribute Continous = new XAttribute("Continous", 0);
                    Loop.Add(Continous);
                }

                XElement Rect = new XElement("Rect");
                Root.Add(Rect);

                    XAttribute Width = new XAttribute("Width", EmitterWNumUpDown.Value);
                    Rect.Add(Width);

                    XAttribute Height = new XAttribute("Height", EmitterHNumUpDown.Value);
                    Rect.Add(Height);

                    XAttribute Left = new XAttribute("Left", emitter.Rect.Left);
                    Rect.Add(Left);

                    XAttribute Right = new XAttribute("Right", emitter.Rect.Right);
                    Rect.Add(Right);

                    XAttribute Top = new XAttribute("Top", emitter.Rect.Top);
                    Rect.Add(Top);

                    XAttribute Bottom = new XAttribute("Bottom", emitter.Rect.Bottom);
                    Rect.Add(Bottom);

                    XAttribute PosX = new XAttribute("PosX", emitter.Rect.X);
                    Rect.Add(PosX);

                    XAttribute PosY = new XAttribute("PosY", emitter.Rect.Y);
                    Rect.Add(PosY);

                XElement Color = new XElement("Color");
                Root.Add(Color);

                    XAttribute StartColorR = new XAttribute("StartColorR", StartColorButton.BackColor.R);
                    Color.Add(StartColorR);

                    XAttribute StartColorG = new XAttribute("StartColorG", StartColorButton.BackColor.G);
                    Color.Add(StartColorG);

                    XAttribute StartColorB = new XAttribute("StartColorB", StartColorButton.BackColor.B);
                    Color.Add(StartColorB);

                    XAttribute EndColorR = new XAttribute("EndColorR", EndColorButton.BackColor.R);
                    Color.Add(EndColorR);

                    XAttribute EndColorG = new XAttribute("EndColorG", EndColorButton.BackColor.G);
                    Color.Add(EndColorG);

                    XAttribute EndColorB = new XAttribute("EndColorB", EndColorButton.BackColor.B);
                    Color.Add(EndColorB);

                    XAttribute StartAlpha = new XAttribute("StartAlpha", StartAlphaNumUpDown.Value);
                    Color.Add(StartAlpha);

                    XAttribute EndAlpha = new XAttribute("EndAlpha", EndAlphaNumUpDown.Value);
                    Color.Add(EndAlpha);

                XElement Image = new XElement("Image");
                Root.Add(Image);

                    XAttribute ParticleImage = new XAttribute("ParticleImage", emitter.ImageFileName1);
                    Image.Add(ParticleImage);

                XElement Offset = new XElement("Offset");
                Root.Add(Offset);

                    XAttribute OffsetX = new XAttribute("OffsetX", emitter.Offset1.X);
                    Offset.Add(OffsetX);

                    XAttribute OffsetY = new XAttribute("OffsetY", emitter.Offset1.Y);
                    Offset.Add(OffsetY);

                for (int i = 0; i < emitter.MaxParticles1; i++)
                {
                    XElement Particles = new XElement("Particles" + i);
                    Root.Add(Particles);

                    XAttribute PartCurrentLife = new XAttribute("MinLife", emitter.Particles[i].CurrentLife);
                        Particles.Add(PartCurrentLife);

                        XAttribute LifeTime = new XAttribute("LifeTime", emitter.Particles[i].LifeTime);
                        Particles.Add(LifeTime);

                        XAttribute Scale = new XAttribute("Scale", emitter.Particles[i].Scale);
                        Particles.Add(Scale);

                        XAttribute VelX = new XAttribute("VelocityX", emitter.Particles[i].Velocity.X);
                        Particles.Add(VelX);

                        XAttribute VelY = new XAttribute("VelocityY", emitter.Particles[i].Velocity.Y);
                        Particles.Add(VelY);

                        XAttribute PartRotation = new XAttribute("PartRotation", emitter.Particles[i].Rotation);
                        Particles.Add(PartRotation);

                        XAttribute CurrColorR = new XAttribute("CurrColorR", emitter.Particles[i].CurrentColor.R);
                        Particles.Add(CurrColorR);

                        XAttribute CurrColorG = new XAttribute("CurrColorG", emitter.Particles[i].CurrentColor.G);
                        Particles.Add(CurrColorG);

                        XAttribute CurrColorB = new XAttribute("CurrColorB", emitter.Particles[i].CurrentColor.B);
                        Particles.Add(CurrColorB);

                        XAttribute CurrColorA = new XAttribute("CurrColorA", emitter.Particles[i].CurrentColor.A);
                        Particles.Add(CurrColorA);

                        XAttribute StartPositonX = new XAttribute("StartPositionX", emitter.Particles[i].StartPosition.X);
                        Particles.Add(StartPositonX);

                        XAttribute StartPositonY = new XAttribute("StartPositionY", emitter.Particles[i].StartPosition.Y);
                        Particles.Add(StartPositonY);

                }

                Root.Save(dlg.FileName);
            }
        }

        private void loadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "All Files|*.*|Xml Files|*.xml";
            dlg.FilterIndex = 2;
            dlg.InitialDirectory = RelativePath;

            if (DialogResult.OK == dlg.ShowDialog())
            {
                XElement Root = XElement.Load(dlg.FileName);

                if (Root != null)
                {
                    
                    XElement Life = Root.Element("Life");
                    if (Life != null)
                    {
                        XAttribute MinLife = Life.Attribute("MinLife");
                        if (MinLife != null)
                        {
                            LifeSpanNumUpDown.Value = decimal.Parse(MinLife.Value);
                            LifeSpanTrackBar.Value = int.Parse(MinLife.Value);
                        }

                        XAttribute MaxLife = Life.Attribute("MaxLife");
                        if (MaxLife != null)
                        {
                            MaxLifeNumUpDown.Value = decimal.Parse(MaxLife.Value);
                            MaxLifeTrackBar.Value = int.Parse(MaxLife.Value);
                        }

                        XAttribute MaxParticles = Life.Attribute("MaxParticles");
                        if (MaxParticles != null)
                        {
                            MaxPartNumUpDown.Value = decimal.Parse(MaxParticles.Value);
                            MaxPartTrackBar.Value = int.Parse(MaxParticles.Value);
                        }
                    }

                    XElement Size = Root.Element("Size");
                    if (Size != null)
                    {
                        XAttribute StartScale = Size.Attribute("StartScale");
                        if (StartScale != null)
                        {
                            StartScaleNumUpDown.Value = decimal.Parse(StartScale.Value);
                            StartScaleTrackBar.Value = int.Parse(StartScale.Value);
                        }

                        XAttribute EndScale = Size.Attribute("EndScale");
                        if (EndScale != null)
                        {
                            EndScaleNumUpDown.Value = decimal.Parse(EndScale.Value);
                            EndScaleTrackBar.Value = int.Parse(EndScale.Value);
                        }
                    }

                    XElement Velocity = Root.Element("Velocity");
                    if (Velocity != null)
                    {
                        XAttribute XVelocity1 = Velocity.Attribute("XVelocity1");
                        if (XVelocity1 != null)
                        {
                            XVelNumUpDown.Value = decimal.Parse(XVelocity1.Value);
                            XVelTrackBar.Value = int.Parse(XVelocity1.Value);
                        }

                        XAttribute XVelocity2 = Velocity.Attribute("XVelocity2");
                        if (XVelocity1 != null)
                        {
                            XVel2NumUpDown.Value = decimal.Parse(XVelocity2.Value);
                            XVel2TrackBar.Value = int.Parse(XVelocity2.Value);
                        }

                        XAttribute YVelocity1 = Velocity.Attribute("YVelocity1");
                        if (YVelocity1 != null)
                        {
                            YVelNumUpDown.Value = decimal.Parse(YVelocity1.Value);
                            YVelTrackBar.Value = int.Parse(YVelocity1.Value);
                        }

                        XAttribute YVelocity2 = Velocity.Attribute("YVelocity2");
                        if (YVelocity2 != null)
                        {
                            YVel2NumUpDown.Value = decimal.Parse(YVelocity2.Value);
                            YVel2TrackBar.Value = int.Parse(YVelocity2.Value);
                        }
                    }

                    XElement Movement = Root.Element("Movement");
                    if (Movement != null)
                    {
                        XAttribute Rotation = Movement.Attribute("Rootation");
                        if (Rotation != null)
                        {
                            RotationNumUpDown.Value = decimal.Parse(Rotation.Value);
                            RotationTrackBar.Value = int.Parse(Rotation.Value);
                        }

                        XAttribute GravityX = Movement.Attribute("GravityX");
                        if (GravityX != null)
                        {
                            GravityXNumUpDown.Value = decimal.Parse(GravityX.Value);
                            GravityXTrackBar.Value = int.Parse(GravityX.Value);
                        }

                        XAttribute GravityY = Movement.Attribute("GravityX");
                        if (GravityY != null)
                        {
                            GravityYNumUpDown.Value = decimal.Parse(GravityY.Value);
                            GravityYTrackBar.Value = int.Parse(GravityY.Value);
                        }
                    }

                    XElement Blend = Root.Element("Blend");
                    if (Blend != null)
                    {

                        XAttribute SourceBlend = Blend.Attribute("SourceBlend");
                        if (SourceBlend != null)
                        {
                            switch (int.Parse(SourceBlend.Value))
                            {
                                case 1:
                                    SourceComboBox.Text = "ZERO";
                                    this.Source = 1;
                                    break;
                                case 2:
                                    SourceComboBox.Text = "ONE";
                                    this.Source = 2;
                                    break;
                                case 3:
                                    SourceComboBox.Text = "SRCCOLOR";
                                    this.Source = 3;
                                    break;
                                case 4:
                                    SourceComboBox.Text = "INVSRCCOLOR";
                                    this.Source = 4;
                                    break;
                                case 5:
                                    SourceComboBox.Text = "SRCALPHA";
                                    this.Source = 5;
                                    break;
                                case 6:
                                    SourceComboBox.Text = "INVSRCALPHA";
                                    this.Source = 6;
                                    break;
                                case 7:
                                    SourceComboBox.Text = "DESTALPHA";
                                    this.Source = 7;
                                    break;
                                case 8:
                                    SourceComboBox.Text = "INVDESTALPHA";
                                    this.Source = 8;
                                    break;
                                case 9:
                                    SourceComboBox.Text = "DESTCOLOR";
                                    this.Source = 9;
                                    break;
                                case 10:
                                    SourceComboBox.Text = "INVDESTCOLOR";
                                    this.Source = 10;
                                    break;
                                case 11:
                                    SourceComboBox.Text = "SRCALPHASAT";
                                    this.Source = 11;
                                    break;
                                case 12:
                                    SourceComboBox.Text = "BOTHSRCALPHA";
                                    this.Source = 12;
                                    break;
                                case 13:
                                    SourceComboBox.Text = "BOTHINVSRCALPHA";
                                    this.Source = 13;
                                    break;
                                case 14:
                                    SourceComboBox.Text = "BLENDFACTOR";
                                    this.Source = 14;
                                    break;
                                case 15:
                                    SourceComboBox.Text = "INVBLENDFACTOR";
                                    this.Source = 15;
                                    break;
                                case 16:
                                    SourceComboBox.Text = "SRCCOLOR2";
                                    this.Source = 16;
                                    break;
                                case 17:
                                    SourceComboBox.Text = "INVSRCCOLOR2";
                                    this.Source = 17;
                                    break;
                            };
                        }

                        XAttribute DestBlend = Blend.Attribute("DestBlend");
                        if (DestBlend != null)
                        {

                            switch (int.Parse(DestBlend.Value))
                            {
                                case 1:
                                    DestComboBox.Text = "ZERO";
                                    this.Dest = 1;
                                    break;
                                case 2:
                                    DestComboBox.Text = "ONE";
                                    this.Dest = 2;
                                    break;
                                case 3:
                                    DestComboBox.Text = "SRCCOLOR";
                                    this.Dest = 3;
                                    break;
                                case 4:
                                    DestComboBox.Text = "INVSRCCOLOR";
                                    this.Dest = 4;
                                    break;
                                case 5:
                                    DestComboBox.Text = "SRCALPHA";
                                    this.Dest = 5;
                                    break;
                                case 6:
                                    DestComboBox.Text = "INVSRCALPHA";
                                    this.Dest = 6;
                                    break;
                                case 7:
                                    DestComboBox.Text = "DESTALPHA";
                                    this.Dest = 7;
                                    break;
                                case 8:
                                    DestComboBox.Text = "INVDESTALPHA";
                                    this.Dest = 8;
                                    break;
                                case 9:
                                    DestComboBox.Text = "DESTCOLOR";
                                    this.Dest = 9;
                                    break;
                                case 10:
                                    DestComboBox.Text = "INVDESTCOLOR";
                                    this.Dest = 10;
                                    break;
                                case 11:
                                    DestComboBox.Text = "SRCALPHASAT";
                                    this.Dest = 11;
                                    break;
                                case 12:
                                    DestComboBox.Text = "BOTHSRCALPHA";
                                    this.Dest = 12;
                                    break;
                                case 13:
                                    DestComboBox.Text = "BOTHINVSRCALPHA";
                                    this.Dest = 13;
                                    break;
                                case 14:
                                    DestComboBox.Text = "BLENDFACTOR";
                                    this.Dest = 14;
                                    break;
                                case 15:
                                    DestComboBox.Text = "INVBLENDFACTOR";
                                    this.Dest = 15;
                                    break;
                                case 16:
                                    DestComboBox.Text = "SRCCOLOR2";
                                    this.Dest = 16;
                                    break;
                                case 17:
                                    DestComboBox.Text = "INVSRCCOLOR2";
                                    this.Dest = 17;
                                    break;
                            };
                        }
                    }

                    XElement Loop = Root.Element("Loop");
                    if (Loop != null)
                    {
                        XAttribute Continous = Loop.Attribute("Continous");
                        if (Continous != null)
                        {
                            int toCheck = int.Parse(Continous.Value);
                            if(toCheck == 1)
                            {
                                this.continous = true;
                            }
                            else if (toCheck == 0)
                            {
                                this.continous = false;
                            }
                        }
                    }

                    XElement Rect = Root.Element("Root");
                    if (Rect != null)
                    {
                        XAttribute Width = Rect.Attribute("Width");
                        if (Width != null)
                        {
                            EmitterWNumUpDown.Value = decimal.Parse(Width.Value);
                            EmitterWTrackBar.Value = int.Parse(Width.Value);
                        }

                        XAttribute Height = Rect.Attribute("Height");
                        if (Height != null)
                        {
                            EmitterHNumUpDown.Value = decimal.Parse(Height.Value);
                            EmitterHTrackBar.Value = int.Parse(Height.Value);
                        }

                        XAttribute PosX = Rect.Attribute("PosX");
                        if (PosX != null)
                        {
                            XPosNumUpDown.Value = decimal.Parse(PosX.Value);
                        }

                        XAttribute PosY = Rect.Attribute("PosY");
                        if (PosY != null)
                        {
                            YPosNumUpDown.Value = decimal.Parse(PosY.Value);
                        }
                    }

                    XElement Colors = Root.Element("Color");
                    if (Colors != null)
                    {
                        XAttribute StartColorR = Colors.Attribute("StartColorR");
                        XAttribute StartColorG = Colors.Attribute("StartColorG");
                        XAttribute StartColorB = Colors.Attribute("StartColorB");
                        XAttribute StartAlpha = Colors.Attribute("StartAlpha");
                        if (StartColorR != null && StartColorG != null && StartColorB != null
                            && StartAlpha != null)
                        {
                            Color srtClr = Color.FromArgb(int.Parse(StartAlpha.Value), int.Parse(StartColorR.Value),
                                int.Parse(StartColorG.Value), int.Parse(StartColorB.Value));

                            StartColorButton.BackColor = srtClr;
                        }

                        XAttribute EndColorR = Colors.Attribute("EndColorR");
                        XAttribute EndColorG = Colors.Attribute("EndColorG");
                        XAttribute EndColorB = Colors.Attribute("EndColorB");
                        XAttribute EndAlpha = Colors.Attribute("EndAlpha");
                        if (EndColorR != null && EndColorG != null && EndColorB != null
                            && EndAlpha != null)
                        {
                            Color endClr = Color.FromArgb(int.Parse(EndAlpha.Value), int.Parse(EndColorR.Value),
                                int.Parse(EndColorG.Value), int.Parse(EndColorB.Value));

                            EndColorButton.BackColor = endClr;
                        }
                    }

                    XElement Image = Root.Element("Image");
                    if (Image != null)
                    {
                        XAttribute ParticleImage = Image.Attribute("ParticleImage");
                        if (ParticleImage != null)
                        {
                            ParticleFile = ParticleImage.Value;
                        }
                    }


                    emitter = new Emitter((int)EmitterWNumUpDown.Value,
                    (int)EmitterHNumUpDown.Value,
                    (int)XPosNumUpDown.Value, (int)YPosNumUpDown.Value,
                    (int)MaxPartNumUpDown.Value,
                    (int)LifeSpanNumUpDown.Value, (int)MaxLifeNumUpDown.Value,
                    (float)StartScaleNumUpDown.Value, (float)EndScaleNumUpDown.Value,
                    (float)XVelNumUpDown.Value, (float)XVel2NumUpDown.Value,
                    (float)YVelNumUpDown.Value, (float)YVel2NumUpDown.Value,
                    (float)RotationNumUpDown.Value, ParticleFile,
                    (int)StartAlphaNumUpDown.Value,
                    (int)EndAlphaNumUpDown.Value);
                }
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.looping = false;
            this.Close();
        }

        // Particle Group Box
        private void LifeSpanTrackBar_Scroll(object sender, EventArgs e)
        {
            if ((float)LifeSpanTrackBar.Value <= (float)MaxLifeTrackBar.Value
                && (float)LifeSpanNumUpDown.Value <= (float)MaxLifeNumUpDown.Value)
            {
                LifeSpanNumUpDown.Value = LifeSpanTrackBar.Value;
            }
        }

        private void LifeSpanNumUpDown_ValueChanged(object sender, EventArgs e)
        {
            if ((float)LifeSpanNumUpDown.Value <= (float)MaxLifeNumUpDown.Value
                && (float)LifeSpanTrackBar.Value <= (float)MaxLifeTrackBar.Value)
            {
                LifeSpanTrackBar.Value = (int)LifeSpanNumUpDown.Value;
            }
        }

        private void StartScaleTrackBar_Scroll(object sender, EventArgs e)
        {
            StartScaleNumUpDown.Value = StartScaleTrackBar.Value;
        }

        private void StartScaleNumUpDown_ValueChanged(object sender, EventArgs e)
        {
            StartScaleTrackBar.Value = (int)StartScaleNumUpDown.Value;
        }

        private void XVelTrackBar_Scroll(object sender, EventArgs e)
        {
            XVelNumUpDown.Value = XVelTrackBar.Value;
        }

        private void XVelNumUpDown_ValueChanged(object sender, EventArgs e)
        {
            XVelTrackBar.Value = (int)XVelNumUpDown.Value;
        }

        private void YVelTrackBar_Scroll(object sender, EventArgs e)
        {
            YVelNumUpDown.Value = YVelTrackBar.Value;
        }

        private void YVelNumUpDown_ValueChanged(object sender, EventArgs e)
        {
            YVelTrackBar.Value = (int)YVelNumUpDown.Value;
        }

        private void MaxPartTrackBar_Scroll(object sender, EventArgs e)
        {
            MaxPartNumUpDown.Value = MaxPartTrackBar.Value;
            emitter.Reset((int)MaxPartNumUpDown.Value);
        }

        private void MaxPartNumUpDown_ValueChanged(object sender, EventArgs e)
        {
            MaxPartTrackBar.Value = (int)MaxPartNumUpDown.Value;
            emitter.Reset(MaxPartTrackBar.Value);
        }

        // Emmiter Group Box
        private void EmitterWTrackBar_Scroll(object sender, EventArgs e)
        {
            EmitterWNumUpDown.Value = EmitterWTrackBar.Value;
            emitter.SetRectWidth(EmitterWTrackBar.Value);
            splitContainer1.Panel1.Invalidate();
        }

        private void EmitterWNumUpDown_ValueChanged(object sender, EventArgs e)
        {
            EmitterWTrackBar.Value = (int)EmitterWNumUpDown.Value;
            emitter.SetRectWidth((int)EmitterWNumUpDown.Value);
            splitContainer1.Panel1.Invalidate();
        }

        private void EmitterHTrackBar_Scroll(object sender, EventArgs e)
        {
            EmitterHNumUpDown.Value = EmitterHTrackBar.Value;
            emitter.SetRectHeight(EmitterHTrackBar.Value);
            splitContainer1.Panel1.Invalidate();
        }

        private void EmitterHNumUpDown_ValueChanged(object sender, EventArgs e)
        {
            EmitterHTrackBar.Value = (int)EmitterHNumUpDown.Value;
            emitter.SetRectHeight((int)EmitterHNumUpDown.Value);
            splitContainer1.Panel1.Invalidate();
        }

        private void RotationTrackBar_Scroll(object sender, EventArgs e)
        {
            RotationNumUpDown.Value = RotationTrackBar.Value;
        }

        private void RotationNumUpDown_ValueChanged(object sender, EventArgs e)
        {
            RotationTrackBar.Value = (int)RotationNumUpDown.Value;
        }

        private void setBackgroundColorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ColorDialog dlg = new ColorDialog();

            if (DialogResult.OK == dlg.ShowDialog())
            {
                splitContainer1.Panel1.BackColor = dlg.Color;
            }
        }

        private void randomizeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            RTB = new RandomizeToolBox();

            if (DialogResult.OK == RTB.ShowDialog())
            {
                RTB.Randomize(LifeSpanNumUpDown.Value, MaxLifeNumUpDown.Value,
                       MaxPartNumUpDown.Value, StartScaleNumUpDown.Value, EndScaleNumUpDown.Value,
                       XVelNumUpDown.Value, XVel2NumUpDown.Value, YVelNumUpDown.Value,
                       YVel2NumUpDown.Value, RotationNumUpDown.Value, GravityXNumUpDown.Value,
                       GravityYNumUpDown.Value, SourceComboBox.Text, DestComboBox.Text,
                       EmitterWNumUpDown.Value, EmitterHNumUpDown.Value, XPosNumUpDown.Value,
                       YPosNumUpDown.Value, StartAlphaNumUpDown.Value, EndAlphaNumUpDown.Value,
                       StartColorButton.BackColor, EndColorButton.BackColor);

                emitter.Update(CurrnetTime, (int)EmitterWNumUpDown.Value, (int)EmitterHNumUpDown.Value,
                (int)XPosNumUpDown.Value, (int)YPosNumUpDown.Value, (int)MaxPartNumUpDown.Value,
                (int)LifeSpanNumUpDown.Value, (int)MaxLifeNumUpDown.Value, (float)StartScaleNumUpDown.Value, (float)EndScaleNumUpDown.Value,
                (float)XVelNumUpDown.Value, (float)XVel2NumUpDown.Value,
                (float)YVelNumUpDown.Value, (float)YVel2NumUpDown.Value,
                (float)RotationNumUpDown.Value, (float)GravityXNumUpDown.Value,
                (float)GravityYNumUpDown.Value, Source, Dest, this.continous, (int)StartAlphaNumUpDown.Value,
                (int)EndAlphaNumUpDown.Value);
            }
            
        }

        private void backgroundToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();

            if (DialogResult.OK == dlg.ShowDialog())
            {
                BGID = SGP.ManagedTextureManager.Instance.LoadTexture(dlg.FileName, 0);
                BGFilePath = dlg.FileName;
            }
        }

        private void loadParticleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.InitialDirectory = RelativePath;

            if (DialogResult.OK == dlg.ShowDialog())
            {
                emitter.ImageID1 = (int)SGP.ManagedTextureManager.Instance.LoadTexture(dlg.FileName,0);
                emitter.ImageFileName1 = dlg.FileName;
                ParticleFile = dlg.FileName;
            }
        }

        private void XPosNumUpDown_ValueChanged(object sender, EventArgs e)
        {
            splitContainer1.Panel1.Invalidate();
        }

        private void YPosNumUpDown_ValueChanged(object sender, EventArgs e)
        {
            splitContainer1.Panel1.Invalidate();
        }

        private void StartColorButton_Click(object sender, EventArgs e)
        {
            ColorDialog dlg = new ColorDialog();

            if (DialogResult.OK == dlg.ShowDialog())
            {
                StartColorButton.BackColor = dlg.Color;

                if (dlg.Color == Color.White)
                {
                    StartColorButton.ForeColor = Color.Black;
                }
                else
                    StartColorButton.ForeColor = Color.White;

                emitter.StartColor1 = dlg.Color;
            }
        }

        private void EndColorButton_Click(object sender, EventArgs e)
        {
            ColorDialog dlg = new ColorDialog();

            if (DialogResult.OK == dlg.ShowDialog())
            {
                EndColorButton.BackColor = dlg.Color;

                if (dlg.Color == Color.White)
                {
                    EndColorButton.ForeColor = Color.Black;
                }
                else
                    EndColorButton.ForeColor = Color.White;
                
                emitter.EndColor1 = dlg.Color;
            }
        }

        private void GravityXTrackBar_Scroll(object sender, EventArgs e)
        {
            GravityXNumUpDown.Value = GravityXTrackBar.Value;
        }

        private void GravityXNumUpDown_ValueChanged(object sender, EventArgs e)
        {
            GravityXTrackBar.Value = (int)GravityXNumUpDown.Value;
        }

        private void GravityYTrackBar_Scroll(object sender, EventArgs e)
        {
            GravityYNumUpDown.Value = GravityYTrackBar.Value;
        }

        private void GravityYNumUpDown_ValueChanged(object sender, EventArgs e)
        {
            GravityYTrackBar.Value = (int)GravityYNumUpDown.Value;
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.looping = false;
        }

        private void graphicsPanel1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Location.X >= emitter.Rect.X && e.Location.X < emitter.Rect.X + emitter.Rect.Width
                && e.Location.Y >= emitter.Rect.Y && e.Location.Y < e.Location.Y + emitter.Rect.Height)
            {
                IsPressed = true;
            }   
        }

        private void graphicsPanel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (IsPressed == true && e.Location.X >= 0 && e.Location.X <= splitContainer1.Panel1.Width
                && e.Location.Y >= 0 && e.Location.Y <= splitContainer1.Panel1.Height)
            {
                XPosNumUpDown.Value = e.Location.X;
                YPosNumUpDown.Value = e.Location.Y;

                splitContainer1.Panel1.Invalidate();
            }
        }

        private void graphicsPanel1_MouseUp(object sender, MouseEventArgs e)
        {
            IsPressed = false;
        }

        private void RootationTrackBar_Scroll_1(object sender, EventArgs e)
        {
            RotationNumUpDown.Value = RotationTrackBar.Value;
        }

        private void RootationNumUpDown_ValueChanged_1(object sender, EventArgs e)
        {
            RotationTrackBar.Value = (int)RotationNumUpDown.Value;
        }

        private void ResetButton_Click(object sender, EventArgs e)
        {
            emitter.Reset((int)MaxPartNumUpDown.Value);
        }

        private void MaxLifeTrackBar_Scroll(object sender, EventArgs e)
        {
            if (MaxLifeTrackBar.Value >= LifeSpanTrackBar.Value)
            { 
                MaxLifeNumUpDown.Value = MaxLifeTrackBar.Value; 
            }
        }

        private void MaxLifeNumUpDown_ValueChanged(object sender, EventArgs e)
        {
            if (MaxLifeNumUpDown.Value >= LifeSpanNumUpDown.Value)
            {
                MaxLifeTrackBar.Value = (int)MaxLifeNumUpDown.Value;
            }
        }

        private void EndScaleTrackBar_Scroll(object sender, EventArgs e)
        {
            EndScaleNumUpDown.Value = EndScaleTrackBar.Value;
        }

        private void EndScaleNumUpDown_ValueChanged(object sender, EventArgs e)
        {
            EndScaleTrackBar.Value = (int)EndScaleNumUpDown.Value;
        }

        private void XVel2TrackBar_Scroll(object sender, EventArgs e)
        {
            XVel2NumUpDown.Value = XVel2TrackBar.Value;
        }

        private void XVel2NumUpDown_ValueChanged(object sender, EventArgs e)
        {
            XVel2TrackBar.Value = (int)XVel2NumUpDown.Value;
        }

        private void YVel2TrackBar_Scroll(object sender, EventArgs e)
        {
            YVel2NumUpDown.Value = YVel2TrackBar.Value;
        }

        private void YVel2NumUpDown_ValueChanged(object sender, EventArgs e)
        {
            YVel2TrackBar.Value = (int)YVel2NumUpDown.Value;
        }

        private void SourceComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch(SourceComboBox.Text)
            {
                case "ZERO":
                    Source = (int)D3DBLEND.D3DBLEND_ZERO;
                    break;
                case "ONE":
                    Source = (int)D3DBLEND.D3DBLEND_ONE;
                    break;
                case "SRCCOLOR":
                    Source = (int)D3DBLEND.D3DBLEND_SRCCOLOR;
                    break;
                case "INVSRCCOLOR":
                    Source = (int)D3DBLEND.D3DBLEND_INVSRCCOLOR;
                    break;
                case "SRCALPHA":
                    Source = (int)D3DBLEND.D3DBLEND_SRCALPHA;
                    break;
                case "INVSRCALPHA":
                    Source = (int)D3DBLEND.D3DBLEND_INVSRCALPHA;
                    break;
                case "DESTALPHA":
                    Source = (int)D3DBLEND.D3DBLEND_DESTALPHA;
                    break;
                case "INVDESTALPHA":
                    Source = (int)D3DBLEND.D3DBLEND_INVDESTALPHA;
                    break;
                case "DESTCOLOR":
                    Source = (int)D3DBLEND.D3DBLEND_DESTCOLOR;
                    break;
                case "INVDESTCOLOR":
                    Source = (int)D3DBLEND.D3DBLEND_INVDESTCOLOR;
                    break;
                case "SRCALPHASAT":
                    Source = (int)D3DBLEND.D3DBLEND_SRCALPHASAT;
                    break;
                case "BOTHSRCALPHA":
                    Source = (int)D3DBLEND.D3DBLEND_BOTHSRCALPHA;
                    break;
                case "BOTHINVSRCALPHA":
                    Source = (int)D3DBLEND.D3DBLEND_BOTHINVSRCALPHA;
                    break;
                case "BLENDFACTOR":
                    Source = (int)D3DBLEND.D3DBLEND_BLENDFACTOR;
                    break;
                case "INVBLENDFACTOR":
                    Source = (int)D3DBLEND.D3DBLEND_INVBLENDFACTOR;
                    break;
                case "SRCCOLOR2":
                    Source = (int)D3DBLEND.D3DBLEND_SRCCOLOR2;
                    break;
                case "INVSRCCOLOR2":
                    Source = (int)D3DBLEND.D3DBLEND_INVSRCCOLOR2;
                    break;
            };
        }

        private void DestComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (DestComboBox.Text)
            {
                case "ZERO":
                    Dest = (int)D3DBLEND.D3DBLEND_ZERO;
                    break;
                case "ONE":
                    Dest = (int)D3DBLEND.D3DBLEND_ONE;
                    break;
                case "SRCCOLOR":
                    Dest = (int)D3DBLEND.D3DBLEND_SRCCOLOR;
                    break;
                case "INVSRCCOLOR":
                    Dest = (int)D3DBLEND.D3DBLEND_INVSRCCOLOR;
                    break;
                case "SRCALPHA":
                    Dest = (int)D3DBLEND.D3DBLEND_SRCALPHA;
                    break;
                case "INVSRCALPHA":
                    Dest = (int)D3DBLEND.D3DBLEND_INVSRCALPHA;
                    break;
                case "DESTALPHA":
                    Dest = (int)D3DBLEND.D3DBLEND_DESTALPHA;
                    break;
                case "INVDESTALPHA":
                    Dest = (int)D3DBLEND.D3DBLEND_INVDESTALPHA;
                    break;
                case "DESTCOLOR":
                    Dest = (int)D3DBLEND.D3DBLEND_DESTCOLOR;
                    break;
                case "INVDESTCOLOR":
                    Dest = (int)D3DBLEND.D3DBLEND_INVDESTCOLOR;
                    break;
                case "SRCALPHASAT":
                    Dest = (int)D3DBLEND.D3DBLEND_SRCALPHASAT;
                    break;
                case "BOTHSRCALPHA":
                    Dest = (int)D3DBLEND.D3DBLEND_BOTHSRCALPHA;
                    break;
                case "BOTHINVSRCALPHA":
                    Dest = (int)D3DBLEND.D3DBLEND_BOTHINVSRCALPHA;
                    break;
                case "BLENDFACTOR":
                    Dest = (int)D3DBLEND.D3DBLEND_BLENDFACTOR;
                    break;
                case "INVBLENDFACTOR":
                    Dest = (int)D3DBLEND.D3DBLEND_INVBLENDFACTOR;
                    break;
                case "SRCCOLOR2":
                    Dest = (int)D3DBLEND.D3DBLEND_SRCCOLOR2;
                    break;
                case "INVSRCCOLOR2":
                    Dest = (int)D3DBLEND.D3DBLEND_INVSRCCOLOR2;
                    break;
            };
        }

        private void emitterRectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IsRectOn = !IsRectOn;
        }

        private void emitterRectColorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ColorDialog dlg = new ColorDialog();

            if (DialogResult.OK == dlg.ShowDialog())
            {
                RectColor = dlg.Color;
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            this.continous = !this.continous;
        }

        private void StartAlphaTrackBar_Scroll(object sender, EventArgs e)
        {
            StartAlphaNumUpDown.Value = StartAlphaTrackBar.Value;
        }

        private void StartAlphaNumUpDown_ValueChanged(object sender, EventArgs e)
        {
            StartAlphaTrackBar.Value = (int)StartAlphaNumUpDown.Value;
        }

        private void EndAlphaTrackBar_Scroll(object sender, EventArgs e)
        {
            EndAlphaNumUpDown.Value = EndAlphaTrackBar.Value;
        }

        private void EndAlphaNumUpDown_ValueChanged(object sender, EventArgs e)
        {
            EndAlphaTrackBar.Value = (int)EndAlphaNumUpDown.Value;
        }
    }
}